<?php
// View for admin - authors users
require_once WACT_ROOT.'controller/controller.inc.php';
require_once WACT_ROOT .'controller/form.inc.php';
require_once WACT_ROOT.'view/view.inc.php';
require_once WACT_ROOT .'view/formview.inc.php';

require_once APP_ROOT . 'view/common/layout.formview.php';
require_once APP_ROOT . 'view/common/layout.view.php';

require_once 'model/common/user.php';
require_once 'model/common/subscription.php';
require_once 'model/common/payment.php';
require_once 'model/common/state.php';
require_once 'model/common/categories.php';

class SubscriberUsersListView extends LayoutFormView {

    function SubscriberUsersListView($TemplateFile) {
        parent :: LayoutFormView($TemplateFile);
    }

    //function supressFormErrors() {
         //$dataset = array (array ());
         //$empty_set =& new ArrayDataSet($list_updated);

    	//$this->Template->setChildDataSource('Summary', $empty_set);
    //}

    function prepare(&$controller, &$request, &$responseModel) {
    	parent::prepare($controller, $request, $responseModel);
         
		$this->Template->set('SITE_CSS',Site::getSiteTemplateCSS());
		$this->Template->set('SITE_BASE',Site::getSiteTemplateBase());
		$this->Template->set('SITE_TITLE',Site::getTitle());
 

		$pager =& $this->Template->getChild('pagenav');
        $list =& User::getSubscriberUsersList($pager);

        $list_updated=array();

        //$list->export();
        //$data =& $list->exportDataSetAsArray();
        $list->reset();
        while ($list->next()) {
        	// adjust active property and photo
        	$dataspace = $list->export();

                $user_id = $list->get('user_id');
        		$active = $list->get('active');
        		//print 'active='.$active;
        		//if (!$active) $list->remove('active');
        		if ($active>0) {

        			//$dataspace = array_merge($dataspace,array('active_show'=>1));
        			$dataspace['active_show'] = 1;
        			//print_r($dataspace); print "\n";
        		}
                //print_r($dataspace);


                //print_r($dataspace);
                $list_updated[$user_id] = $dataspace;
        }
        //print_r($data);

        $list_source_updated =& new ArrayDataSet($list_updated);
        //print_r($list_updated);

        $this->Template->setChildDataSource('List', $list_source_updated);

        $active = $responseModel->get('active');
        if ($active>0) $responseModel->set('active_show',1);

        $photo_id = $responseModel->get('photo_id');
        //print $photo_id;
        //$photo_id = $list->get('photo_id');
        if ($photo_id>0) {
           //$dataspace = array_merge($dataspace,array('photo_show'=>1));
           $responseModel->set('photo_show',1);
           $path = $responseModel->get('path');
           $fname = $responseModel->get('filename_local');
           $fname_thumb = $responseModel->get('filename_thumb');

           $photo_src = "/".APP_NAME."/".$path."/".$fname;
           $photo_src_thumb = "/".APP_NAME."/".$path."/".$fname_thumb;
           $responseModel->set('image_path',$photo_src);
           $responseModel->set('image_path_thumb',$photo_src_thumb);
                    //$dataspace['image_path'] = "photo1.bmp";

        }

        //print_r($responseModel);

        // populate states
        $state = $responseModel->get('state');
        $states =& $this->Template->getChild('state');
        $states_list =& State::getStates();
        $states->setChoices($states_list);
        $states->setSelection($state);


    }


}


// view for handling the list of subscriptions
class SubscriptionsListView extends LayoutView {

	function SubscriptionsListView($TemplateFile) {
        parent :: LayoutView($TemplateFile);
    }

    function prepare(&$controller, &$request, &$responseModel) {
    	parent::prepare($controller, $request, $responseModel);

    	$user_id = $request->getPostProperty('user_id');
    	//print $user_id;
        $pager =& $this->Template->getChild('pagenav');
        //print "prepare the datasource";
        $list =& Subscription::getListForUser($user_id, $pager);

        $list_updated=array();


        $list->reset();
        while ($list->next()) {
        	// adjust active property and photo
        	$dataspace = $list->export();
        	//print_r($dataspace);

                $subscription_id = $list->get('id');
                $user_id = $list->get('user_id');
                $login = $list->get('login');
                $firstname = $list->get('firstname');
                $lastname = $list->get('lastname');

                $type = $list->get('type');
                $type_show = Subscription::getHumanType($type);
                $status = $list->get('status');
                $status_show = Subscription::getHumanStatus($status);
                $dataspace['type_show'] = $type_show;
                $dataspace['status_show'] = $status_show;

                $list_updated[$subscription_id] = $dataspace;
                //print_r($list_updated);
        }


        $list_source_updated =& new ArrayDataSet($list_updated);

        $template =& $this->Template;
        $template->setChildDataSource('List', $list_source_updated);

        $template->set('firstname', $firstname);
        $template->set('lastname', $lastname);
        $template->set('user_id', $user_id);
        $template->set('login', $login);
    }

}

// view for handling the list of payments for the subscription
class SubscriptionPaymentsListView extends LayoutView {

	function SubscriptionPaymentsListView($TemplateFile) {
        parent :: LayoutView($TemplateFile);
    }

    function prepare(&$controller, &$request, &$responseModel) {
    	parent::prepare($controller, $request, $responseModel);

    	//$user_id = $request->getPostProperty('user_id');
        $subscription_id = $request->getPostProperty('id');
        //$payment_id = $request->getPostProperty('payment_id');
    	//print $user_id;
        $pager =& $this->Template->getChild('pagenav');
        //print "prepare the datasource";
        $list =& Payment::getListForSubscription($subscription_id, $pager);

        $list_updated=array();


        $list->reset();
        while ($list->next()) {
        	// adjust active property and photo
        	$dataspace = $list->export();
        	//print_r($dataspace);

                $subscription_id = $list->get('id');
                $payment_id = $list->get('payment_id');
                $user_id = $list->get('user_id');
                $login = $list->get('login');
                $firstname = $list->get('firstname');
                $lastname = $list->get('lastname');

                $type = $list->get('type');
                $type_show = Subscription::getHumanType($type);
                $status = $list->get('status');
                $status_show = Payment::getHumanStatus($status);
                $dataspace['type_show'] = $type_show;
                $dataspace['status_show'] = $status_show;

                $list_updated[$payment_id] = $dataspace;
                //print_r($list_updated);
        }


        $list_source_updated =& new ArrayDataSet($list_updated);

        $template =& $this->Template;
        $template->setChildDataSource('List', $list_source_updated);

        $template->set('firstname', $firstname);
        $template->set('lastname', $lastname);
        $template->set('user_id', $user_id);
        $template->set('login', $login);

        $header_title = 'Payment history for: subscription - '.$type_show.' ('.$status_show.') ';

        $template->set('header_title', $header_title);
    }

}

// view for handling the list of payments for the user
class UserPaymentsListView extends LayoutView {

	function UserPaymentsListView($TemplateFile) {
        parent :: LayoutView($TemplateFile);
    }

    function prepare(&$controller, &$request, &$responseModel) {
    	parent::prepare($controller, $request, $responseModel);

    	//$user_id = $request->getPostProperty('user_id');
        $user_id = $request->getPostProperty('user_id');
        //$payment_id = $request->getPostProperty('payment_id');
    	//print $user_id;
        $pager =& $this->Template->getChild('pagenav');
        //print "prepare the datasource";
        $list =& Payment::getListForUser($user_id, $pager);

        $list_updated=array();


        $list->reset();
        while ($list->next()) {
        	// adjust active property and photo
        	$dataspace = $list->export();
        	//print_r($dataspace);

                $subscription_id = $list->get('id');
                $payment_id = $list->get('payment_id');
                $user_id = $list->get('user_id');
                $login = $list->get('login');
                $firstname = $list->get('firstname');
                $lastname = $list->get('lastname');

                $type = $list->get('type');
                $type_show = Subscription::getHumanType($type);
                $status = $list->get('status');
                $status_show = Payment::getHumanStatus($status);
                $dataspace['type_show'] = $type_show;
                $dataspace['status_show'] = $status_show;

                $list_updated[$payment_id] = $dataspace;
                //print_r($list_updated);
        }


        $list_source_updated =& new ArrayDataSet($list_updated);

        $template =& $this->Template;
        $template->setChildDataSource('List', $list_source_updated);

        $template->set('firstname', $firstname);
        $template->set('lastname', $lastname);
        $template->set('user_id', $user_id);
        $template->set('login', $login);

        $header_title = 'Payment history for: user - '.$firstname.' '.$lastname;

        $template->set('header_title', $header_title);
    }

}


?>